package com.luoye.lj.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.Normalizer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Configuration
@WebFilter(filterName = "Character-Encoding-Filter", urlPatterns = {"/bll/**","/system/**"})
public class MyCharacterEncodingFilter implements Filter {
    private Logger logger= LoggerFactory.getLogger(MyCharacterEncodingFilter.class);
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest)request;
		String servletPath=request1.getServletPath();
		logger.debug("doFilter-request->"+request.getCharacterEncoding());

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Map parameterMap = request.getParameterMap();
		Set  parameterSet=  parameterMap.keySet();
		Iterator parameterIterator = parameterSet.iterator();
		Map hashMap = new HashMap();

		try {
			Map map = new HashMap();
			//规范化操作
			while (parameterIterator.hasNext()) {
				Object key = parameterIterator.next();
				key = String.valueOf(key);
				String[] object = (String[]) parameterMap.get(key);
				for (int i = 0; i < object.length; i++) {
					object[i] = Normalizer.normalize(object[i],Normalizer.Form.NFKC);
				}
				map.put(key, object);
			}

			Set set =  map.keySet();
			Iterator iterator = set.iterator();
			if(servletPath.lastIndexOf("system/wiki") != -1){
				while (iterator.hasNext()) {
					Object key = iterator.next();
					key = String.valueOf(key);
					String[] object = (String[]) map.get(key);
					for (int i = 0; i < object.length; i++) {
						object[i] = object[i].trim();
						object[i] = object[i].replace("<script", "&lt;script");
						object[i] = object[i].replace("script/>", "script/&gt;");
						object[i] = object[i].replace("<style", "&lg;style");
						object[i] = object[i].replace("style/>", "style/&gt;");
						object[i] = object[i].replace("<iframe", "&lt;iframe");
						object[i] = object[i].replace("iframe/>", "iframe/&gt;");
					}
					hashMap.put(key, object);
				}
			}else {
				while (iterator.hasNext()) {
					Object key = iterator.next();
					key = String.valueOf(key);
					String[] object = (String[]) map.get(key);
					for (int i = 0; i < object.length; i++) {
						object[i] = object[i].trim();
						object[i] = object[i].replace("<", "&lt;");
						object[i] = object[i].replace(">", "&gt;");
					}
					hashMap.put(key, object);
				}
			}
		} catch (Exception e) {
			logger.warn(e.toString());
		}
		/**
		 * 由于servletrequest标准是无法改变传入参数，所以重写了个request用来修改。
		 * */
 		MyHttpRequestWrapper myHttpRequestWrapper = new MyHttpRequestWrapper((HttpServletRequest)request);
		myHttpRequestWrapper.setParameterMap(hashMap);
		chain.doFilter(myHttpRequestWrapper, response);
//        logger.info("response->"+response.getCharacterEncoding());
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
